
import requests
import re
from util.headers import get_header
import os,shutil
from util.logger import get_looger

loger=get_looger('高清壁纸.log',__file__)
url = 'https://pic.netbian.com/e/search/result/index.php?page=0&searchid=1224'

url1='https://www.hdbz.net'
root='static/壁纸/'
if os.path.exists(root):
	shutil.rmtree(root)
os.makedirs(root)

loger.info('爬虫启动...')
try:
	res_thumb=requests.get(url1,headers=get_header())
	res_thumb_data=res_thumb.content.decode()
	# print(res_thumb_data)
	thumb_data=re.findall(r'<a href="(/wallpaper/\d{6}/\d{6}.html)" target="_blank" title="(.*?)".*?class="item">.*?<img.*?data-original="(.*?)".*?class="lazy" />',res_thumb_data,re.S)
	# print(len(thumb_data))
	for item in thumb_data:
		detail_url=item[0]
		title=item[1]
		thumb_img_url=item[2]
		thumb_img = requests.get(thumb_img_url).content
		with open(f'{root}{title}_thumb.jpg','wb')as f:
			f.write(thumb_img)
		loger.info(f'爬取{title}的缩略图成功')
		print(item)
		detail_url_all=f'{url1}{detail_url}'
		detail_data=requests.get(detail_url_all).content.decode()
		# print(detail_data)
		detail_img=re.search(r'<div class="pic-preview">.*?<img alt=.*?src="(.*?)"',detail_data,re.S).group(1)
		preview_img=requests.get(detail_img).content
		with open(f'{root}{title}_preview.jpg','wb')as f:
			f.write(preview_img)
		loger.info(f'爬取{title}的预览图成功')
except Exception as e:
	loger.error(e)
loger.info('爬虫结束')